package org.jeecg.modules.commu;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.timeout.IdleStateHandler;
import lombok.Data;
import org.jeecg.modules.driver.DriverPortocal1_0;
import org.jeecg.modules.driver.DriverPortocal1_0_ByteDataDecoder;
import org.jeecg.modules.driver.DriverPortocal1_0_MsgToByteEncoder;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * @author Gjing
 * 客户端初始化器
 **/
public class NettyClientInitializer extends ChannelInitializer<SocketChannel> {

    NettyClientHandler handler = new NettyClientHandler();

    @Override
    protected void initChannel(SocketChannel socketChannel) throws Exception {
//        socketChannel.pipeline().addLast("decoder", new StringDecoder());
//        socketChannel.pipeline().addLast("encoder", new StringEncoder());
        //TODO: make it usable to all driver
        socketChannel.pipeline().addLast(new IdleStateHandler(60,0,0, TimeUnit.SECONDS));
        socketChannel.pipeline().addLast("decoder", new DriverPortocal1_0_ByteDataDecoder());
        socketChannel.pipeline().addLast("encoder",new DriverPortocal1_0_MsgToByteEncoder());
        socketChannel.pipeline().addLast(handler);

    }

    public NettyClientHandler getNettyClinetHandler(){
        return handler;
    }
}
